package com.landian.demo.distributedlock.springboot.db;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author caojl
 * @create 2018/03/17
 **/
public class ProductDB {

    private static Map<String, Long> dataMap = new HashMap<>();

    static {
        long total = 10L;
        dataMap.put("101",total);
        dataMap.put("102",total);
        dataMap.put("103",total);
    }

    /**
     * 获取商品库存
     * @param productId
     * @return
     */
    public static long getStock(String productId){
        return dataMap.getOrDefault(productId, 0L);
    }

    /**
     * 减少商品库存
     * @param productId
     * @return
     */
    public static long decreaseStock(String productId){
        long stock = getStock(productId);
        return dataMap.put(productId, --stock);
    }
}
